package com.google.android.apps.mytracks.util;

import static com.google.android.apps.mytracks.MyTracksConstants.TAG;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

public class AppMetadata implements AppVersion {
  private static final String PACKAGE_NAME = "com.google.android.maps.mytracks";

  private String versionName;
  private int versionCode;

  public AppMetadata(Context context) {
    PackageInfo packageInfo = null;
    try {
      packageInfo = context.getPackageManager().getPackageInfo(PACKAGE_NAME,
          PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
      Log.w(TAG, "Failed to get PackageInfo", e);
    }

    versionName = packageInfo == null ? "unknown" : packageInfo.versionName;
    versionCode = packageInfo == null ? -1 : packageInfo.versionCode;
  }

  public String getVersionName() {
    return versionName;
  }

  public int getVersionCode() {
    return versionCode;
  }
}
